{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "列表推导式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0, 2, 4, 6, 8]\n",
      "[(1, 4), (1, 5), (2, 4), (2, 5), (3, 4), (3, 5)]\n",
      "[<function <lambda> at 0x00000000051789D8>, [0, 1, 2, 3, 4]]\n"
     ]
    }
   ],
   "source": [
    "a=[x*2 for x in range(5)]\n",
    "print(a)\n",
    "\n",
    "print([(x,y) for x in [1,2,3] for y in [4,5]])\n",
    "\n",
    "print([lambda x:x*x, list(range(5))]) #λ函数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "map"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0, 1, 4, 9, 16]\n",
      "[0, 1, 4, 9, 16]\n"
     ]
    }
   ],
   "source": [
    "def square(n):\n",
    "    return n*n\n",
    "c = list(map(square, list(range(5))))\n",
    "print(c)\n",
    "\n",
    "d = list(map(lambda x:x*x, list(range(5))))\n",
    "print(d)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "filter"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0, 2, 4, 6, 8]\n"
     ]
    }
   ],
   "source": [
    "def isEven(n):\n",
    "    return not n%2\n",
    "\n",
    "print(list(filter(isEven, list(range(10)))))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "reduce"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5050\n"
     ]
    }
   ],
   "source": [
    "from functools import reduce\n",
    "\n",
    "print(reduce(lambda x,y: x+y, list(range(101))))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
